package com.zyf.ppx.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.widget.NestedScrollView
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.AbsListView
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.example.zzh.foldtext.SpannableFoldTextView
import com.google.gson.Gson
import com.jaeger.ninegridimageview.NineGridImageViewAdapter
import com.zyf.ppx.R
import com.zyf.ppx.adapter.CommentAdapter
import com.zyf.ppx.bean.CellComments
import com.zyf.ppx.bean.MuitImage
import com.zyf.ppx.bean.Note
import com.zyf.ppx.presenter.CommentPresenter
import com.zyf.ppx.utils.DisplayUtils
import com.zyf.ppx.utils.ViewUtils
import com.zyf.ppx.view_interface.NetInterface
import kotlinx.android.synthetic.main.activity_details.*


class DetailsActivity : AppCompatActivity() , NetInterface<CellComments> {
    var note: Note?=null
    var name =""
    var image_url = ""
    var item_id = ""
    var item_type = 1
    var width = 0
    var height  = 0
    var adapter: CommentAdapter?=null
    var presenter: CommentPresenter?= null
    var offset = 0
    var load = true
    var options: RequestOptions = RequestOptions()
        .centerCrop()
        //.placeholder(R.mipmap.ic_launcher_round)
        .error(android.R.drawable.stat_notify_error)
        .priority(Priority.HIGH)
        .fitCenter()
        //.skipMemoryCache(true)
        .diskCacheStrategy(DiskCacheStrategy.DATA)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        setContentView(R.layout.activity_details)
        if(ViewUtils.videoplayer!=null){
            root.addView(ViewUtils.videoplayer)
            iv_ngrid_layout?.visibility =View.GONE
            gifView?.visibility =View.GONE
            isGif?.visibility =View.GONE

        }
        presenter= CommentPresenter(this)
        val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
        width = windowManager.defaultDisplay.width
        height = windowManager.defaultDisplay.height
        name = intent.getStringExtra("name")
        image_url = intent.getStringExtra("image")
        item_id = intent.getStringExtra("item_id")
        item_type = intent.getIntExtra("item_type",1)
        nick.text = name
        Glide.with(this).load(image_url).apply(RequestOptions.bitmapTransform(
            CircleCrop()
        )).into(header_image)
        recycle.isNestedScrollingEnabled = false
        recycle.layoutManager = LinearLayoutManager(this)
        scroll.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { v, _, scrollY, _, _ ->
            if (scrollY == (v.getChildAt(0).measuredHeight - v.measuredHeight)) {
                if(load){
                    presenter?.getComment(offset,item_id)
                }
            }
        })
        initView()
        back.setOnClickListener { finish() }
        presenter?.getComment(offset,item_id)
    }

    private fun initView() {
        if (item_type == 1) {
            note = Gson().fromJson(intent.getStringExtra("data"),Note::class.java)
            if(TextUtils.isEmpty(note?.text)){
                content.visibility= View.GONE
            }else{
                content.visibility= View.VISIBLE
            }
            content.text = note?.text
            var imageUrl = ArrayList<String>()
            note?.multi_image?.forEach {
                imageUrl.add(it.url_list[0].url)
            }
            content.text = note?.text
            if (imageUrl.size == 1) {
                var multi = note?.multi_image!![0]
                var params: RelativeLayout.LayoutParams = gifView?.layoutParams as RelativeLayout.LayoutParams
                params.leftMargin = 0
                DisplayUtils.dp2px(this, 16.0f)
                if (multi.height < multi.width) {
                    params.width = width
                    params.height = (((width / (multi.width * 1.0f)) * multi.height).toInt())
                } else {
                    if (multi.width < width) {
                        params.width = multi.width
                        params.height = multi.height
                        if (multi.width < width / 2) {
                            params.width = (width * 0.6f).toInt()
                            params.height = (multi.height * ((width * 0.6f) / multi.width)).toInt()
                        }
                        params.leftMargin = DisplayUtils.dp2px(this, 16.0f)
                    } else {
                        params.width = width
                        params.height = (multi.width / width) * multi.height
                        Log.e("比例", (multi.height / (multi.width * 1.0f)).toString())
                        if ((multi.height / (multi.width * 1.0f)) > 1.7f) {
                            params.width = (width * 0.7f).toInt()
                            params.height = (0.7f * multi.height).toInt()
                            params.leftMargin = DisplayUtils.dp2px(this, 16.0f)
                        }
                        if ((multi.height / (multi.width * 1.0f)) > 1.3f) {
                            params.width = (width * 0.8f).toInt()
                            params.height = (0.8f * multi.height).toInt()
                            params.leftMargin = DisplayUtils.dp2px(this, 16.0f)
                        }
                    }
                }

                Log.e("宽高", "params.width" + params.width + ":params.height" + params.height)
                gifView.layoutParams = params

                if (multi.is_gif) {
                    isGif?.visibility = View.VISIBLE

                } else {
                    isGif?.visibility = View.GONE
                }
                if (gifView != null) {
                    Glide.with(this).load(multi.download_list[0].url).apply(options).thumbnail(0.1f).into(gifView)
                }
                gifView.setOnClickListener {
                    var intent = Intent(this@DetailsActivity, ImageActivity::class.java)
                    intent.putExtra("pos", 0)
                    var imageData = ArrayList<MuitImage>()
                    imageData.add(multi)
                    intent.putExtra("data", imageData)
                    startActivity(intent)
                }
                gifView?.visibility = View.VISIBLE
                iv_ngrid_layout?.visibility = View.GONE
            } else {
                gifView?.visibility = View.GONE
                isGif?.visibility = View.GONE
                iv_ngrid_layout?.visibility = View.VISIBLE
                iv_ngrid_layout?.setAdapter(object : NineGridImageViewAdapter<String>() {
                    override fun onDisplayImage(context: Context?, imageView: ImageView?, t: String?) {
                        Glide.with(context!!).load(t).thumbnail(0.1f).into(imageView!!)
                    }

                    override fun onItemImageClick(context: Context?, index: Int, list: MutableList<String>?) {
                        var intent = Intent(this@DetailsActivity, ImageActivity::class.java)
                        intent.putExtra("pos", index)
                        intent.putExtra("data", note?.multi_image as ArrayList<MuitImage>)
                        startActivity(intent)
                    }
                })
                iv_ngrid_layout?.setImagesData(imageUrl)
            }
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        ViewUtils.videoplayer = null
    }

    override fun result(data: CellComments) {
        load = data.has_more
        offset += 10
        if(adapter!=null){
            adapter?.data?.addAll(data.cell_comments)
            adapter?.notifyDataSetChanged()
        }else{
            adapter = CommentAdapter(data.cell_comments)
            recycle?.adapter = adapter
        }
    }

    override fun error(error: String?) {

    }
}
